package com.xbongbong.saas.enums.dictionary;

import com.xbongbong.i18n.util.I18nMessageUtil;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 回款、付款收款类型
 *
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/1/25 11:12
 * @since v1.0
 */
@Getter
public enum PaymentStatusEnum {
    // 回款/付款类型
    UN_RECEIVABLES("1", "", "enum.paymentStatus.alias.un_receivables"),
    RECEIVABLES("2", "", "enum.paymentStatus.alias.receivables"),
    PARTIAL_RECEIPTS("3", "", "enum.paymentStatus.alias.partial_receipts"),
//    REFUNDED("4", "", "已退款"),
    BAD_DEBT("5", "isBad", "enum.paymentStatus.alias.bad_debt"),
    PARTIAL_BAD_DEBT("6", "partialBad", "enum.paymentStatus.alias.partial_bad_debt"),
    RED_RECEIVABLES("7","redReceivables","enum.paymentStatus.alias.red_receivables"),
    UNKNOWN("404", "", "enum.paymentStatus.alias.unknown"),
    ;

    private String code;
    private String name;
    private String alias;

    PaymentStatusEnum(String code, String name, String alias) {
        this.code = code;
        this.name = name;
        this.alias = alias;
    }

    /**
     * 获取未收款状态合集，此处包含：未收款、部分收款
     * @return java.util.List<java.lang.Integer> 未收款code集合
     * @author zcp
     * @date 2019/3/3 15:40
     * @since v1.0
     * @version v1.0
     */
    public static List<String> getUnStatusIn(){
        //未收款、部分收款
        return Arrays.asList(UN_RECEIVABLES.code, PARTIAL_RECEIPTS.code);
    }
    
    /**
     * 获取坏账
     * @param status
     * @return
     */
    public static Integer getBad(String status){
        Integer isBad = 0;
        if ( Arrays.asList(BAD_DEBT.code, PARTIAL_BAD_DEBT.code).contains(status) ){
            isBad = 1;
        }
        return isBad;
    }
    /**
     * 不能合并回款的应收款状态则返回true：坏账、部分坏账、红冲
     * @param status
     * @return boolean
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    public static boolean noMerge(String status){
        return Arrays.asList(BAD_DEBT.code, PARTIAL_BAD_DEBT.code, RED_RECEIVABLES.code).contains(status);
    }
    /**
     * 通过枚举 code 获得枚举
     *
     * @param code 枚举Code
     *
     * @return PaymentStatusEnum
     */
    public static PaymentStatusEnum getByCode(String code) {
        for (PaymentStatusEnum cacheCode : values()) {
            if (code.equals(cacheCode.getCode())) {
                return cacheCode;
            }
        }
        return UNKNOWN;
    }

    public static boolean redPermission(String code) {
        return Objects.equals(code,RECEIVABLES.getCode()) || Objects.equals(code,PARTIAL_RECEIPTS.getCode()) || Objects.equals(code,PARTIAL_BAD_DEBT.getCode());
    }

    public static boolean addPermission(String code) {
        return Objects.equals(code,UN_RECEIVABLES.getCode()) || Objects.equals(code,PARTIAL_RECEIPTS.getCode());
    }

    public static boolean invoicePermission(String code) {
        return Objects.equals(code,UN_RECEIVABLES.getCode()) || Objects.equals(code,PARTIAL_RECEIPTS.getCode()) || Objects.equals(code,RECEIVABLES.getCode());
    }
    
    public static PaymentStatusEnum getByAlias(String alias) {
        for (PaymentStatusEnum cacheCode : values()) {
            if (alias.equals(cacheCode.getAlias())) {
                return cacheCode;
            }
        }
        return UNKNOWN;
    }

    /**
     * 获取应收款中预计回款日期不能编辑的状态的列表
     *
     * @return
     */
    public static List<String> getPaymentUnEditStatus() {
        return Arrays.asList(RECEIVABLES.getCode(), BAD_DEBT.getCode(), PARTIAL_BAD_DEBT.getCode(), RED_RECEIVABLES.getCode());
    }

    /**
     * 判断该状态的应收款是否可以编辑应收金额
     * @return
     */
    public static Boolean checkPaymentEditStatus(String status) {
       if(Objects.equals(status,UN_RECEIVABLES.getCode())){
           return true;
       }else {
           return false;
       }
    }
    public String getAlias() {
        return I18nMessageUtil.getMessage(alias);
    }
}
